package com.kolon.business.mVolunteer.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mVolunteer.controller.htmlTestController")
public class htmlTestController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mVolunteer/html_test.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		///test
		// 밑에 이게 주소여?
		//http://203.225.3.52:8000/Contribute.aspx?Gubun=1&CompanyCode=KNC&YMD=201205
		String url = MsgUtil.getMessage("xml.url");//http://203.225.3.52:8000요렇게 나와
		System.out.println("TEST_URL : "+url);//http://203.225.3.52:8000 <- 이거 박힌거여? 아니면 ㅇㅇ때때로 바뀌는거여?
		//박힌거일껄..
		
		
		
		String ymd = "201205";
		
		
		
		// 세션
				HttpSession httpSession = request.getSession();
				Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
				String id = (String)userMap.get("userId");
				String pw = (String)userMap.get("userPw");
				String busoCode = (String)userMap.get("DeptCode");
				String type = ComUtil.nullChk(request.getParameter("type"), "KOLONCOM");
				String page = ComUtil.nullChk(request.getParameter("page"), "1");
				int pageInt = ComUtil.sToi(page) * 15;
				
				System.out.println("TEST_ID :"+id);
				System.out.println("TEST_PW : "+pw);
				System.out.println("TEST_CODE : "+busoCode);
				System.out.println("type :"+type);
				System.out.println("page :"+page);
				System.out.println("pageInt :"+pageInt);
				System.out.println("news");
				
				String CompanyCode = "";
				if("AFF".equals(type)) {
					CompanyCode = (String)userMap.get("CompanyCD");
				}else{
					CompanyCode = "KOLONCOM";
				}
				// 뉴스회사코드별 게시판코드얻기
				StringBuffer buffer = new StringBuffer();
				buffer.append(url); 
				buffer.append("/Contribute.aspx?Gubun=1");
				buffer.append("&companyCode=").append("BNT");
				//buffer.append("&companyCode=").append(CompanyCode);
				// 밑에껏도 있어야되?
				//buffer.append("&busoCode=").append(busoCode);
				buffer.append("&YMD=").append(ymd);
				
				
				System.out.println("url toString : "+buffer.toString());
				// XML DOCUMENT 및 인증 처리
				Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
				Map map = new HashMap();
		System.out.println("doc : "+doc);
		
		/*이러 로그가 이케나와CompanyCD=LS, 이게 회사코드
		{DeptCode=LC20SUB, TitleName=주임, EMPNUM=ETEST5, 
		UserAccount=etest5, EmpNum=ETEST5, UserName=EKPTest5, 
		COMPANYCD=LS, DEPTNAME=관계사/테스트, MOBILENUM=011-111-1115, 
		TITLECODE=T0011, OfficeTelNum=02-2222-1233, CompanyCD=LS, userId=etest5,
		DEPTCODE=LC20SUB, connDate=201205, TitleCode=T0011, TITLENAME=주임,
		MobileNum=011-111-1115, conciseGrade=, COMPANYNAME=코오롱베니트, USERACCOUNT=etest5,
		companyname=코오롱베니트, DeptName=관계사/테스트, userPw=etest5, 
		OFFICETELNUM=02-2222-1233, USERNAME=EKPTest5}
		 */
		
		Element element2 = doc.getRootElement();
		List xmlList2 = element2.getChildren("Table");
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		// XML 데이터 
		while (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map = new HashMap();
			map.put("SERVICE_DATE", el.getChild("SERVICE_DATE").getText());
			map.put("SERVICE_START_TIME", el.getChild("SERVICE_START_TIME").getText());
			map.put("SERVICE_END_TIME", el.getChild("SERVICE_END_TIME").getText());
			map.put("SERVICE_CODE", el.getChild("SERVICE_CODE").getText());
			//map.put("Title", ComUtil.subString(el.getChild("Title").getText(), 21));
			map.put("SERVICE_NAME", el.getChild("SERVICE_NAME").getText());
			map.put("POSS_YN", el.getChild("POSS_YN").getText());
			map.put("POSS_NUMBER", el.getChild("POSS_NUMBER").getText());
			
			mapList2.add(map);
		}
		
		System.out.println("mapList2.get : "+mapList2.get(0));
		// 도메인
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/business/mVolunteer/html_test");
		//modelAndView.addObject("mapList2", mapList2);
		System.out.println("111");
		return modelAndView;
	}
}

/*
@Controller("com.kolon.business.mKolonNews.controller.MKolonNewsListController")
public class MKolonNewsListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mKolonNews/mKolonNewsList.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		System.out.println("TEST_URL : "+url);
		if(url != null)System.out.println("URL:"+url);
		
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String busoCode = (String)userMap.get("DeptCode");
		String type = ComUtil.nullChk(request.getParameter("type"), "KOLONCOM");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		int pageInt = ComUtil.sToi(page) * 15;
		
		System.out.println("TEST_ID :"+id);
		System.out.println("TEST_PW : "+pw);
		System.out.println("TEST_CODE : "+busoCode);
		System.out.println("type :"+type);
		System.out.println("page :"+page);
		System.out.println("pageInt :"+pageInt);
		System.out.println("news");
		
		String CompanyCode = "";
		if("AFF".equals(type)) {
			CompanyCode = (String)userMap.get("CompanyCD");
		}else{
			CompanyCode = "KOLONCOM";
		}
		// 뉴스회사코드별 게시판코드얻기
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/kolon_news.aspx?mode=").append("1");
		buffer.append("&companyCode=").append(CompanyCode);
		buffer.append("&busoCode=").append(busoCode);

		System.out.println("22222");
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		Map map = new HashMap();
		// XML 데이터 
		map = new HashMap();
		map.put("BoardCD", element.getValue());
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		StringBuffer buffer2 = new StringBuffer();
		buffer2.append(url);
		buffer2.append("/kolon_news.aspx?mode=").append("2");
		buffer2.append("&BoardCD=").append(map.get("BoardCD"));//417438
		buffer2.append("&SortFieldName=").append("SortCreateDate");
		buffer2.append("&SortAscDesc=").append("desc");
		buffer2.append("&Page=").append("1");
		buffer2.append("&CompanyCode=").append(CompanyCode);
		buffer2.append("&ViewRowCount=").append(pageInt);
		buffer2.append("&userid=").append(id);
		buffer2.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc2 = ComUtil.getDocument(buffer2.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren("BoardList");
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		// XML 데이터 
		while (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map = new HashMap();
			map.put("ArticleCD", el.getChild("ArticleCD").getText());
			map.put("UserID", el.getChild("UserID").getText());
			map.put("UserName", el.getChild("UserName").getText());
			map.put("BoardCD", el.getChild("BoardCD").getText());
			//map.put("Title", ComUtil.subString(el.getChild("Title").getText(), 21));
			map.put("Title", el.getChild("Title").getText());
			map.put("UCreateDate", el.getChild("UCreateDate").getText());
			map.put("ExpireDate", el.getChild("ExpireDate").getText());
			map.put("ViewCnt", el.getChild("ViewCnt").getText());
			map.put("FileOnly", el.getChild("FileOnly").getText());
			map.put("Importance", el.getChild("Importance").getText());
			map.put("AttachCNT", el.getChild("AttachCNT").getText());
			map.put("CommentCNT", el.getChild("CommentCNT").getText());
			map.put("Ref_Seq", el.getChild("Ref_Seq").getText());
			map.put("Ref_Level", el.getChild("Ref_Level").getText());
			map.put("SubCnt", el.getChild("SubCnt").getText());
			map.put("IsNotice", el.getChild("IsNotice").getText());
			map.put("B_ACL", el.getChild("B_ACL").getText());
			mapList2.add(map);
		}
		// TableInfo 영역
		Document doc3 = ComUtil.getDocument(buffer2.toString(), id, pw);
		Element element3 = doc3.getRootElement();
		List xmlList3 = element3.getChildren("TableInfo");
		Iterator itXml3 = xmlList3.iterator();
		// XML 데이터 
		if (itXml3.hasNext()) {
			Element el = (Element)itXml3.next();
			map = new HashMap();
			map.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
			map.put("TotalNoticeRecortCnt", el.getChild("TotalNoticeRecortCnt").getText());
			map.put("TotalPage", el.getChild("TotalPage").getText());
			map.put("CurrPage", el.getChild("CurrPage").getText());
			map.put("OnePageCnt", el.getChild("OnePageCnt").getText());
			map.put("bPublicWrite", el.getChild("bPublicWrite").getText());
			map.put("UserID", el.getChild("UserID").getText());
			map.put("SAdminCNT", el.getChild("SAdminCNT").getText());
		}
		// 리턴
		modelAndView.addObject("mapList2", mapList2);
		modelAndView.addObject("map3", map);
		modelAndView.addObject("page", page);
		modelAndView.addObject("type", type);
		return modelAndView;
	}

}
*/